package middleware

import (
	log "github.com/cihub/seelog"
	"github.com/go-redis/redis"
	"matching/conf"
	"time"
)

var RedisClient *redis.Client

func Init() {
	addr := conf.GetString("redis.addr")
	password := conf.GetString("redis.pwd")
	db := conf.GetInt("redis.db")
	RedisClient = redis.NewClient(&redis.Options{
		Addr:     addr,
		Password: password, // no password set
		DB:       db,  // use default DB
	})

	_, err := RedisClient.Ping().Result()
	if err != nil {
		panic(err)
	} else {
		log.Infof("Connected to redis: %s success", addr)
	}
}

func RedisSet(key string,value interface{}, expiration time.Duration)  {
	RedisClient.Set(key,value,expiration)
}

func RedisGet(key string) string {
	return RedisClient.Get(key).Val()
}

